package com.yndsoft.jc.utils.selectphoto;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.yndsoft.jc.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class SelectPhotoAdapter extends BaseAdapter {

    public boolean isSingle() {
        return isSingle;
    }

    public void setSingle(boolean single) {
        isSingle = single;
    }

    private boolean isSingle = false;
    private HashMap<Integer, View> viewMap;
    /**
     * 用户选择的图片，存储为图片的完整路径
     */
    public static List<String> mSelectedImage = new LinkedList<String>();
    public List<String> mDatas = new ArrayList<String>();
    private Context mContext;

    public static List<String> getmSelectedImage() {
        return mSelectedImage;
    }

    @Override
    public int getCount() {
        return mDatas.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            return "";
        } else {
            return mDatas.get(position - 1);
        }
    }

    interface TakePhoto {
        void takePhoto();

        void selectedCounts(int count);
    }

    private TakePhoto takePhoto;

    public void setTakePhoto(TakePhoto takePhoto) {
        this.takePhoto = takePhoto;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public SelectPhotoAdapter(Context mContext, List<String> mDatas) {
        this.mContext = mContext;
        this.mDatas.clear();
        this.mDatas.addAll(mDatas);
        mSelectedImage.clear();
        viewMap = new HashMap<Integer, View>();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (!viewMap.containsKey(position) || viewMap.get(position) == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.select_photo_grid_item, null);
            holder.id_item_image = (ImageView) convertView.findViewById(R.id.id_item_image);
            holder.iv_camera = (ImageView) convertView.findViewById(R.id.iv_camera);
            holder.id_item_select = ((ImageButton) convertView.findViewById(R.id.id_item_select));
            convertView.setTag(holder);
            viewMap.put(position, convertView);
        } else {
            convertView = viewMap.get(position);
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == 0) {
            //设置no_pic
            holder.id_item_image.setVisibility(View.GONE);
            //设置no_selected
            holder.id_item_select.setVisibility(View.GONE);
            holder.iv_camera.setVisibility(View.VISIBLE);
            holder.iv_camera.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    takePhoto.takePhoto();
                }
            });
        } else {
            //设置no_pic
            holder.id_item_image.setImageResource(R.mipmap.pictures_no);
            //设置no_selected
            holder.id_item_select.setImageResource(R.mipmap.pictures_unselected);
            Glide.with(mContext).load(mDatas.get(position - 1)).into(holder.id_item_image);
            holder.id_item_image.setColorFilter(null);
            //设置ImageView的点击事件
            final ViewHolder finalHolder = holder;
            holder.id_item_select.setOnClickListener(new View.OnClickListener() {
                //选择，则将图片变暗，反之则反之
                @Override
                public void onClick(View v) {

                    if (isSingle == false) {
                        // 已经选择过该图片
                        if (mSelectedImage.contains(getItem(position))) {
                            mSelectedImage.remove(getItem(position));
                            finalHolder.id_item_select.setImageResource(R.mipmap.pictures_unselected);
                            finalHolder.id_item_image.setColorFilter(null);
                        } else
                        // 未选择该图片
                        {
                            mSelectedImage.add((String) getItem(position));
                            finalHolder.id_item_select.setImageResource(R.mipmap.pictures_selected);
                            finalHolder.id_item_image.setColorFilter(Color.parseColor("#77000000"));
                        }

                        takePhoto.selectedCounts(mSelectedImage.size());
                    } else {
                        // 已经选择过该图片
                        if (mSelectedImage.contains(getItem(position))) {
                            mSelectedImage.remove(getItem(position));
                            finalHolder.id_item_select.setImageResource(R.mipmap.pictures_unselected);
                            finalHolder.id_item_image.setColorFilter(null);
                        } else
                        // 未选择该图片
                        {
                            mSelectedImage.clear();
                            mSelectedImage.add((String) getItem(position));
                            finalHolder.id_item_select.setImageResource(R.mipmap.pictures_selected);
                            finalHolder.id_item_image.setColorFilter(Color.parseColor("#77000000"));
                            notifyDataSetChanged();
                        }
                    }

                }
            });

            /**
             * 已经选择过的图片，显示出选择过的效果
             */
            if (mSelectedImage.contains(getItem(position))) {
                finalHolder.id_item_select.setImageResource(R.mipmap.pictures_selected);
                finalHolder.id_item_image.setColorFilter(Color.parseColor("#77000000"));
            }
        }

        if (viewMap.size() > 20) {
            synchronized (convertView) {
                for (int i = 1; i < ((GridView) parent).getFirstVisiblePosition() - 3; i++) {
                    viewMap.remove(i);
                }
                for (int i = ((GridView) parent).getLastVisiblePosition() + 3; i < getCount(); i++) {
                    viewMap.remove(i);
                }
            }
        }

        return convertView;
    }

    class ViewHolder {
        ImageView id_item_image;
        ImageView iv_camera;
        ImageButton id_item_select;
        RelativeLayout rl_parent;
    }

}
